package JUC.pool;

import java.util.concurrent.*;

/**
 * @author mubei
 * @date 2021/11/21
 * @description
 * Executors  工具类
 */
public class Demo01 {

    public static void main(String[] args) {
        // 单一线程
        //ExecutorService pool = Executors.newSingleThreadExecutor();
        //ExecutorService pool = Executors.newFixedThreadPool(5);
        ExecutorService pool = Executors.newCachedThreadPool();

        // 自定义线程池
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(2,
                5,
                3,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );


        try{
            for (int i = 0; i < 10; i++) {
                poolExecutor.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"ok");
                });
            }
        }finally{
            pool.shutdown();
        }
    }
}
